﻿using BusinessDB;
using BusinessResource;
using BusinessService;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using Excel = Microsoft.Office.Interop.Excel;

namespace TemplateGenerator
{
    public abstract class AbstractSpecGenerator : AbstractGenerator
    {
        public byte[] Generate(ProdDB prod, bool convertPDF = false)
        {
            Excel.Application application = null;

            Excel.Workbook wb = null;
            string exportFile = null;
            try
            {
                string dateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                exportFile = TemplateName + dateTime + ".xlsx";
                string sourceFile = Path.Combine(ServerConfig.Singleton.TemplateRoot, TemplateName + ".xlsx");
                string temFile = Path.Combine(ServerConfig.Singleton.ExportRoot, exportFile);
                File.Copy(sourceFile, temFile, true);

                application = new Excel.Application();
                application.Visible = false;

                wb = application.Workbooks.Open(temFile);
                Excel.Worksheet sheet = wb.Sheets[1];
                Excel.XmlMap map = SearchMap(wb, SpecMapName);


                if (!string.IsNullOrEmpty(prod.PicFacade))
                {
                    string imageUrl = prod.PicFacade.Remove(0, ServerConfig.Singleton.ImgUrl.Length);
                    imageUrl = Path.Combine(ServerConfig.Singleton.ImgRoot, imageUrl);
                    MappingRange(sheet, FacadeXpath, map, imageUrl);
                }



                if (!string.IsNullOrEmpty(prod.PicSize))
                {
                    string imageUrl = prod.PicSize.Remove(0, ServerConfig.Singleton.ImgUrl.Length);
                    imageUrl = Path.Combine(ServerConfig.Singleton.ImgRoot, imageUrl);
                    MappingRange(sheet, SizeXpath, map, imageUrl);
                }

                if (!string.IsNullOrEmpty(prod.PicGrading))
                {
                    string imageUrl = prod.PicGrading.Remove(0, ServerConfig.Singleton.ImgUrl.Length);
                    imageUrl = Path.Combine(ServerConfig.Singleton.ImgRoot, imageUrl);
                    MappingRange(sheet, GradingXpath, map, imageUrl);
                }

                if (!string.IsNullOrEmpty(prod.PicLive))
                {
                    string imageUrl = prod.PicLive.Remove(0, ServerConfig.Singleton.ImgUrl.Length);
                    imageUrl = Path.Combine(ServerConfig.Singleton.ImgRoot, imageUrl);
                    MappingRange(sheet, LiveXpath, map, imageUrl);
                }

                string xmlData = ConvertToXml(prod);
                map.ImportXml(xmlData);

                wb.Save();
                if (convertPDF)
                {
                    exportFile = TemplateName + dateTime + ".pdf";
                    //wb.SaveAs(pdfFile, Excel.XlFixedFormatType.xlTypePDF);
                    wb.ExportAsFixedFormat(
                    Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
                    Path.Combine(ServerConfig.Singleton.ExportRoot, exportFile),
                    Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard,
                    false,
                    true,
                    1,
                    1,
                    false,
                    System.Reflection.Missing.Value);
                    try
                    {
                        File.Delete(temFile);
                    }
                    catch
                    {
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                try
                {
                    wb.Close();
                    Marshal.ReleaseComObject(wb);
                }
                catch
                {
                }

                try
                {
                    application.Quit();
                    Marshal.ReleaseComObject(application);
                    Kill(application);
                }
                catch
                {
                }
            }

            exportFile = Path.Combine(ServerConfig.Singleton.ExportUrl, exportFile);
            return Encoding.UTF8.GetBytes(exportFile);
        }

        protected abstract string ConvertToXml(ProdDB prodb);

    }
}
